<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :disabled="selectMenu.yx.length === 1" :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.ssyxm" >
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{i.dwh+"-"+  i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:2}" style="text-align:center">
            <span class="table-page-search-submitButtons" >
            <a-button type="primary" @click="loadData">查询</a-button>
            </span>
          </a-col>
           <a-col :sm="{span:0}" :lg="{span:0}" :xl="{span:8}" style="text-align:center">
         </a-col>
           <a-col :sm="{span:0}" :lg="{span:0}" :xl="{span:8}" style="text-align:center">
         </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :loading="loading"
      :dataSource="data"
      :pagination="pagination"
      @change="handleTableChange"
    >
      <template slot="kmmc" slot-scope="text,record">
        <a @click="handlePreview(record.id)">{{text}}</a>
      </template>

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>
    </a-table><!-- 详情 -->
    <reference-detail ref="referenceDetail" :info="preViewData" ></reference-detail>
  </a-card>
</template>

<script>
// import moment from 'moment'
import ReferenceDetail from '../reference/referenceDetail'
import { STable } from '~'
import { mapState } from 'vuex'

const selectArr = ['TYPE0126', 'TYPE0075', 'TYPE0125', 'TYPE0072']

export default {
  components: {
    STable,
    ReferenceDetail
  },
  data () {
    return {
      loading: false,
      data: [],
      // 查询参数
      queryParam: {
        ssyxm: ''
      },
      postParams: {},
      // 表头
      columns: [
        {
          title: '序号',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '科目码',
          dataIndex: 'kmbm',
          width:80
        },
        {
          title: '科目名称',
          dataIndex: 'kmmc',
          scopedSlots: { customRender: 'kmmc' },
          width:250
        },
        {
          title: '所属院系',
          dataIndex: 'ssyxm'
        }
      ],
      selectMenu: {
        yx: [] // 院系
      },
      referenceManageEditData: {},
      preViewData: {},
      pagination: {
        current: 1
      }
    }
  },
  computed: {
    ...mapState({
      systemInfo: state => state.app.systemInfo,
      TYPE0126: state => state.app['TYPE0126'] // 年级
    })
  },
  created () {
    this.initSelectMenu()
    selectArr.forEach(v => {
      this.$store.dispatch('app/setDictionary', v)
    })
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
        this.queryParam.ssyxm = res[0].data[0].dwh
        this.loadData({
          pageSize: 10,
          pageNum: 1
        })
      })
    },
      // 加载数据
    loadData(parameter = {}) {
      if(!parameter.pageNum){
        this.pagination.current = 1
        parameter.pageNum = 1
      }
      let params = Object.assign(this.queryParam, parameter)
      this.loading = true;
      this.$api.recruitstat.enterySubjectCheckList(params).then(res => {
        // console.log(res)
         const pagination = { ...this.pagination
        };
        pagination.total = 0
        if (res.data && res.data.total) {
          pagination.total = res.data.total;
        }
        this.data = []
        this.pagination = pagination;
        if (res.data && res.data.rows.length) {
            res.data.rows.map(item=>{
            if(item.ssyxm){

              item.ss=item.ssyxm.split(',')

              item.newss= item.ss.map(_item=>{

               this.selectMenu.yx.map(key=>{
                    if(key.dwh==_item){
                      _item=key.dwmc
                      // item.sn.push(key.dwmc)

                    }
                  })
                return _item
               })
               item.ssyxm=item.newss.join(',')
            }
            return item
          })
          this.data = res.data.rows;
        }
        this.loading = false;
      })
    },
    // 分页
    handleTableChange(pagination, filters, sorter) {
      console.log(pagination);
      const pager = { ...this.pagination
      };
      pager.current = pagination.current;
      this.pagination = pager;
      this.loadData({
        pageSize: 10,
        pageNum: pagination.current
      });
    },
    // // 选择院系
    // yxChange (code) {
    //   this.$set(this.queryParam, 'sszym', '')
    //   this.$api.base.majorAll({lsdwh: code}).then(res => {
    //     this.$set(this.selectMenu, 'zy', res.data)
    //   })
    // },
    // 查看详情
    handlePreview (key) {
       this.$api.recruitstat.entrySubjectManageId(key).then( res => {
          this.preViewData = res.data
           this.$refs.referenceDetail.show()
       })
    }
  }
}
</script>
